package com.dyh.quickdemo.lock;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/*
@Author=Joey Wheeler
@Date=2021/3/29 13:09
*/
public class M {
    ReentrantLock reentrantLock = new ReentrantLock();
        Logger logger = LoggerFactory.getLogger(this.getClass());
    @Test
    public void test() throws InterruptedException {


        new Thread(()->{
            try {
                logger.info("{} {}",
                        Thread.currentThread().getName(),
                        reentrantLock.tryLock(100L, TimeUnit.MILLISECONDS));
                Thread.sleep(500L);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                reentrantLock.unlock();
            }
        }).start();

        new Thread(()->{
            try {
                logger.info("{} {}",
                        Thread.currentThread().getName(),
                        reentrantLock.tryLock(100L, TimeUnit.MILLISECONDS));

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


        Thread.sleep(10000L);
    }
}
